package zuoye;

import util.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class Zhucejpanel extends JPanel implements ActionListener {
    private Mainjframe frame;
    private Icon sureimage = Util.getIcon("/images/buttonSure.jpg");
    private Icon offimage = Util.getIcon("/images/buttonOff.jpg");
    private JButton SurejButton;
    private JButton OffjButton;
    private JLabel Zhuuser;
    private JLabel Zhupwd;
    private JLabel Zhumoney;
    private JTextField Zhuceuser;
    private JTextField Zhucepwd;
    private JTextField Zhucemoney;

    public Zhucejpanel(Mainjframe frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);
        SurejButton = new JButton(sureimage);
        SurejButton.setBounds(360, 500, 140, 50);
        this.add(SurejButton);
        SurejButton.addActionListener(this);
        OffjButton = new JButton(offimage);
        OffjButton.setBounds(700, 500, 140, 50);
        this.add(OffjButton);
        OffjButton.addActionListener(this);

        Zhumoney = new JLabel("请输入您的本金：");
        Zhumoney.setForeground(Color.BLACK);
        Zhumoney.setFont(new Font("宋体", Font.BOLD, 22));
        Zhumoney.setBounds(340, 160, 200, 40);
        this.add(Zhumoney);

        Zhucemoney = new JTextField();
        Zhucemoney.setBounds(550, 160, 350, 40);
        this.add(Zhucemoney);

        Zhuuser = new JLabel("请输入用户名：");
        Zhuuser.setForeground(Color.BLACK);
        Zhuuser.setFont(new Font("宋体", Font.BOLD, 22));
        Zhuuser.setBounds(340, 220, 200, 40);
        this.add(Zhuuser);

        Zhuceuser = new JTextField();
        Zhuceuser.setBounds(550, 220, 350, 40);
        this.add(Zhuceuser);

        Zhupwd = new JLabel("请输入密码：");
        Zhupwd.setForeground(Color.BLACK);
        Zhupwd.setFont(new Font("宋体", Font.BOLD, 22));
        Zhupwd.setBounds(340, 280, 200, 40);
        this.add(Zhupwd);

        Zhucepwd = new JTextField();
        Zhucepwd.setBounds(550, 280, 350, 40);
        this.add(Zhucepwd);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == SurejButton) {
            ArrayList<User> user = (ArrayList<User>) FileUtil.readObject("User.dat");
            double a = Double.valueOf(Zhucemoney.getText());
            if (Zhuceuser.getText().matches("^[a-zA-Z][a-zA-Z0-9_]{4,15}$")) {
                if (Zhucepwd.getText().matches("^[a-zA-Z]\\w{5,17}$")) {
                    JOptionPane.showMessageDialog(this, "注册成功");
                    user.add(new User(Zhuceuser.getText(), Zhucepwd.getText(), a));
                    FileUtil.writeObject("User.dat", user);
                    frame.switchPanel("login");
                } else {
                    JOptionPane.showMessageDialog(this, "密码格式不匹配");
                    Zhucepwd.setText("");
                }
            } else {
                JOptionPane.showMessageDialog(this, "用户名格式不匹配");
                Zhuceuser.setText("");
            }
        } else if (e.getSource() == OffjButton) {
            frame.switchPanel("login");
        }
    }
}
